package main

import (
	"bytes"
	"devServer/Util"
	"encoding/json"
	"fmt"
	_ "fmt"
	"io"
	"net/http"
	_ "net/http"
	"os/exec"
	"strings"
	"time"
)

var devPath = "/mnt/c/Users/Administrator/IdeaProjects/data-manage/data-api/target/"
var prodPath = "/data/cmttg/testmanage/"
var path = prodPath

func readLog(w http.ResponseWriter, r *http.Request) {
	lines, _ := Util.ReadLines(path + "/api.out")
	result := ResultJson{Code: 1000}
	result.Data = lines
	marshal, _ := json.Marshal(result)
	_, _ = io.WriteString(w, string(marshal))
}

func status(w http.ResponseWriter, r *http.Request) {
	lines, _ := Util.ReadLines(path + "api.out")
	result := getStatusByLines(lines)
	marshal, _ := json.Marshal(result)
	_, _ = io.WriteString(w, string(marshal))
}
func getStatusByLines(lines []string) ResultJson {
	//获取当前日期字符串
	dateStr := time.Now().Format("2006-01-02")
	//判断lines是否包含当前日期字符串
	isIncludeCurrentDate := false
	isIncludeError := false
	isIncludeSuccess := false
	for _, line := range lines {
		if (isIncludeCurrentDate && isIncludeSuccess) || (isIncludeCurrentDate && isIncludeError) {
			break
		}
		if !isIncludeCurrentDate {
			isIncludeCurrentDate = strings.Contains(line, dateStr)
		}
		if !isIncludeError {
			isIncludeError = strings.Contains(line, "ERROR")
		}
		if !isIncludeSuccess {
			isIncludeSuccess = strings.Contains(line, "项目启动成功")
		}
	}
	hasPid := serviceStatus()
	if hasPid && isIncludeCurrentDate {
		if !isIncludeError && isIncludeSuccess {
			result := ResultJson{Code: 1000, Data: 2}
			return result
		}
		if isIncludeError {
			result := ResultJson{Code: 1000, Data: 3}
			return result
		}
		result := ResultJson{Code: 1000, Data: 1}
		return result
	}
	if hasPid {
		if lines != nil || len(lines) > 0 {
			result := ResultJson{Code: 1000, Data: 2}
			return result
		}
		result := ResultJson{Code: 1000, Data: 1}
		return result
	}
	result := ResultJson{Code: 1000, Data: 0}
	return result
}
func serviceStatus() bool {
	actionCmd := exec.Command("bash", "-c", " cd "+path+" \n "+path+"run.sh  status ")
	var actionOut bytes.Buffer
	actionCmd.Stdout = &actionOut
	err := actionCmd.Run()
	return err == nil
}

func executeShell(w http.ResponseWriter, r *http.Request) {
	// 如果是预检请求，直接返回
	if r.Method == http.MethodOptions {
		return
	}
	//获取参数
	result := ResultJson{Code: 1000}
	q := r.URL.Query()
	action := q.Get("action")
	// 调用shell命令
	pwd := exec.Command("bash", "-c", " pwd ")
	var currentDir bytes.Buffer
	pwd.Stdout = &currentDir
	_ = pwd.Run()
	fmt.Println(currentDir.String())

	actionCmd := exec.Command("bash", "-c", " cd "+path+" \n "+path+"run.sh  "+action+" ")
	var actionOut bytes.Buffer
	actionCmd.Stdout = &actionOut
	err := actionCmd.Run()
	fmt.Println(actionOut.String())
	result.ActionOut = actionOut.String()
	if action == "status" {
		result.Code = 1000
		if err != nil {
			result.Data = 0
		} else {
			result.Data = 1
		}
	}
	if action == "stop" {
		result.Data = 4
		if err == nil {
			result.Data = 0
		}
	}
	if action == "restart" {
		result.Data = 1
		if err != nil {
			result.Data = 0
		}
	}
	if action == "start" {
		result.Data = 1
		if err != nil {
			result.Data = 0
		}
	}
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		result.Code = 500
		result.Message = err.Error()
	} else {
		result.Message = action + "执行成功"
	}
	marshal, _ := json.Marshal(result)
	_, _ = io.WriteString(w, string(marshal))
	fmt.Println(string(marshal))
}
